home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2 Examples.sit
/
Raven 1.2 Examples
/
Quill
/
Source
/
CustomClasses.cpp
< prev
next >
Wrap
Text File
|
1997-09-03
|
7KB
|
283 lines
/*
* File: CustomClasses.cpp
* Summary: A class containing information about custom pane classes.
* Written by: Jesse Jones
*
* Copyright ゥ 1996-1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <2> 4/26/97 JDJ Added GetCreator.
* <1> 10/14/96 JDJ Created
*/
#include "CustomClasses.h"
#include <Map.h>
#include <Resources.h>
#include <ZApplication.h>
#include <ZDialogBox.h>
#include <ZDialogHandler.h>
#include <ZExceptions.h>
#include <ZHandleStream.h>
#include <ZInput.h>
#include <ZLocker.h>
#include <ZResUtils.h>
#include <ZTextBox.h>
// ===================================================================================
// Internal Functions
// ===================================================================================
//---------------------------------------------------------------
//
// GetClassName
//
//---------------------------------------------------------------
static string GetClassName(const string& oldName)
{
TDialogBox* dialog = TDialogBox::Create(207, TApplication::Instance());
TTextBox* textBox = dynamic_cast<TTextBox*>(dialog->FindSubPane("Class Name"));
textBox->SetText(oldName);
textBox->SelectAll();
dialog->SetLatentTarget(textBox);
string message = kNothingMessage;
{
TDialogHandler handler(dialog);
dialog->Show();
while (message != kOKMessage)
message = handler.ProcessNextEvent();
}
string name = textBox->GetText();
return name;
}
#pragma mark -
// ===================================================================================
// class CCustomClasses
// ===================================================================================
//---------------------------------------------------------------
//
// CCustomClasses::~CCustomClasses
//
//---------------------------------------------------------------
CCustomClasses::~CCustomClasses()
{
delete mTable;
}
//---------------------------------------------------------------
//
// CCustomClasses::CCustomClasses
//
//---------------------------------------------------------------
CCustomClasses::CCustomClasses()
{
mTable = new ClassTable;
}
//---------------------------------------------------------------
//
// CCustomClasses::GetNumClasses
//
//---------------------------------------------------------------
long CCustomClasses::GetNumClasses() const
{
return mTable->size();
}
//---------------------------------------------------------------
//
// CCustomClasses::HasClass
//
//---------------------------------------------------------------
bool CCustomClasses::HasClass(const string& derivedClass) const
{
ClassTable::const_iterator iter = mTable->find(derivedClass);
return !(iter == mTable->end()); // CW Pro 1 has problems finding operator!= when precompiling SGI STL
}
//---------------------------------------------------------------
//
// CCustomClasses::GetBaseClass
//
//---------------------------------------------------------------
string CCustomClasses::GetBaseClass(const string& derivedClass) const
{
string baseClass;
ClassTable::const_iterator iter = mTable->find(derivedClass);
if (!(iter == mTable->end())) // CW Pro 1 has problems finding operator!= when precompiling SGI STL
baseClass = (*iter).second;
else
throw TDomainException(derivedClass + LoadAppString(" isn't one of the known custom types."));
return baseClass;
}
//---------------------------------------------------------------
//
// CCustomClasses::AddClass
//
//---------------------------------------------------------------
void CCustomClasses::AddClass(const string& derivedClass, const string& baseClass)
{
ClassTable::iterator iter = mTable->find(derivedClass);
if (iter == mTable->end()) {
mTable->insert(ClassEntry(derivedClass, baseClass));
CreatorProcPtr creator = this->GetCreator(baseClass);
TReanimator::Instance()->UnregisterClass(derivedClass);
TReanimator::Instance()->RegisterClass(derivedClass, creator);
this->Broadcast(this);
} else {
ClassEntry& entry = *iter;
if (entry.second != baseClass) {
entry.second = baseClass;
CreatorProcPtr creator = this->GetCreator(baseClass);
TReanimator::Instance()->UnregisterClass(derivedClass);
TReanimator::Instance()->RegisterClass(derivedClass, creator);
this->Broadcast(this);
}
}
}
//---------------------------------------------------------------
//
// CCustomClasses::RemoveClass
//
// Note that we do not unregister the derived class because other
// documents may still be using it.
//
//---------------------------------------------------------------
void CCustomClasses::RemoveClass(const string& derivedClass)
{
ASSERT(mTable->count(derivedClass) == 1);
mTable->erase(derivedClass);
this->Broadcast(this);
}
//---------------------------------------------------------------
//
// CCustomClasses::ReadResources
//
//---------------------------------------------------------------
void CCustomClasses::ReadResources()
{
this->DisableBroadcasting();
// Zap any old names that may be hanging around
while (mTable->size() > 0) {
ClassTable::iterator iter = mTable->begin();
this->RemoveClass((*iter).first);
}
// and read in the new resources.
short num = ::Count1Resources(kType);
ASSERT(num <= 1);
for (short index = 1; index <= num; index++) {
Handle hand = ::Get1IndResource(kType, index);
ThrowIfResFail(hand);
TInHandleStream stream(hand); // stream takes the handle
while (!stream.AtEnd()) {
string derived, base;
stream >> derived >> base;
this->AddClass(derived, base);
}
}
this->EnableBroadcasting();
this->Broadcast(this);
}
//---------------------------------------------------------------
//
// CCustomClasses::WriteResources
//
// ・・・ハrewrite this so that an error while writing the new data
// ・・・ハout doesn't trash all of the old data
//
//---------------------------------------------------------------
void CCustomClasses::WriteResources(ResID id)
{
// Out with the old...
Delete1Resource(kType);
// and in with the new.
TOutHandleStream stream;
ClassTable::iterator iter = mTable->begin();
while (iter != mTable->end()) {
ClassEntry& entry = *iter++;
stream << entry.first << entry.second;
}
{
THandle hand = stream.GetHandle();
TLocker lock(hand);
WriteResource(kType, id, hand.GetPtr(), hand.GetSize());
}
}
#pragma mark ハ
//---------------------------------------------------------------
//
// CCustomClasses::GetCreator
//
//---------------------------------------------------------------
CreatorProcPtr CCustomClasses::GetCreator(const string& className)
{
CreatorProcPtr creator = nil;
try {
creator = TReanimator::Instance()->GetCreator(className);
} catch (const TReanimateException& e) {
string name = ::GetClassName(className);
creator = TReanimator::Instance()->GetCreator(name);
}
return creator;
}